package com.example.springboot.service;

import com.example.springboot.Mapper.CategoryMapper;
import com.example.springboot.pojo.Category;
import com.example.springboot.pojo.CategoryExample;
import com.example.springboot.request.CategoryQueryRequest;
import com.example.springboot.request.CategorySaveRequest;
import com.example.springboot.response.CategoryQueryResponse;
import com.example.springboot.response.PageResponse;
import com.example.springboot.util.CopyUtil;
import com.example.springboot.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ServiceCategory {

    private static final Logger LOG= LoggerFactory.getLogger(ServiceCategory.class);

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    //这里把原来的list方法去掉了分页功能，一次查出所有数据也就不需要分页了，改名为all方法。
    public List<CategoryQueryResponse> all(){

        CategoryExample categoryExample = new CategoryExample();
        //这里调用了Example类的设置顺序的方法，“sort asc”是代表按照sort的值顺序asc（排列）
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList=categoryMapper.selectByExample(categoryExample);

        //列表复制
        List<CategoryQueryResponse> list= CopyUtil.copyList(categoryList, CategoryQueryResponse.class);
        return list;
    }

    /*
    * 保存
    * */
    public void save(CategorySaveRequest request){
        Category category=CopyUtil.copy(request,Category.class);
        if(ObjectUtils.isEmpty(request.getId())){
            //新增
            category.setId(snowFlake.nextId());
            categoryMapper.insert(category);
        }
        else{
            //更新
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    /*
    * 删除
    * */
    public void delete(Long id){
        categoryMapper.deleteByPrimaryKey(id);

    }

    public PageResponse<CategoryQueryResponse> list(CategoryQueryRequest categoryQueryRequest){



        CategoryExample categoryExample = new CategoryExample();
        //这里调用了Example类的设置顺序的方法，“sort asc”是代表按照sort的值顺序asc（排列）
        categoryExample.setOrderByClause("sort asc");
        CategoryExample.Criteria criteria = categoryExample.createCriteria();



        PageHelper.startPage(categoryQueryRequest.getPage(), categoryQueryRequest.getSize());
        List<Category> categoryList=categoryMapper.selectByExample(categoryExample);


        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        LOG.info("总行数："+pageInfo.getTotal());
        LOG.info("总页数：" + +pageInfo.getPages());



        /*List<CategoryResponse> responseList=new ArrayList<>();
        for( Category category:categoryList){
            *//*CategoryResponse categoryResponse=new CategoryResponse();
            BeanUtils.copyProperties(category,categoryResponse);*//*

            //使用CopyUtil代替上面的那两句
        CategoryResponse categoryResponse = CopyUtil.copy(category,CategoryResponse.class);


            responseList.add(categoryResponse);

        }*/


        //列表复制
        List<CategoryQueryResponse> list= CopyUtil.copyList(categoryList, CategoryQueryResponse.class);

        PageResponse<CategoryQueryResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);
        return pageResponse;
    }
}
